const { mongoose } = require("../util/mongoose");

let userSchema = mongoose.Schema({
    name: { require: true, type: String },
    pwd: { required: true, type: String },
    phone: String,
    email: String,
    nickname: String,
    des:String
})
let User = mongoose.model("users", userSchema);
//注册
const regist = (query = {}) => {
    let collection = new User(query);
    return collection.save()
        .then(res => res)
        .catch(err => {
            console.log(err);
            return false;
        })
}
//查找
const find = (query = {}) => {
    return User.find(query)
        .then(res => res)
        .catch(err => {
            console.log(err);
            return false;
        })
}
//修改个人信息, 修改密码也包含在这里,修改密码的是通过id找到用户,然后验证旧密码成功后再修改
const update = (query, updated) => {
    return User.updateOne(query, updated)
        .then(res => res)
        .catch(err => {
            console.log(err);
            return false
        })
}
//通过id查找用户
const findtoid = (query = {}) => {
    return User.find(query)
        .then(res => res)
        .catch(err => {
            console.log(err);
            return false;
        })
}

module.exports = {
    regist, find, update, findtoid
}